package com.yjps.house.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yjps.common.message.Message;
import com.yjps.common.util.TimeUtil;
import com.yjps.house.model.House;
import com.yjps.house.service.HouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/house")
public class HouseController {

    @Autowired
    HouseService houseService;

    /**
     * 更新房源信息
     * @param json "house": { "id":"","buildingId": "", "title": "", "description": "", "imageUrl": "", "area": "", "price": "", "sellType": "", "detail": "" }
     * @return
     */
    @RequestMapping(value = "/update.do",method = RequestMethod.POST)
    @ResponseBody
    public String update(@RequestParam(value = "json") String json,HttpServletResponse respone){
        JSONObject object= JSON.parseObject(json);
        House house = JSON.parseObject(object.getString("house"), House.class);
        boolean flag=houseService.updatehouse(house);
        Message message;
        if (flag){
            message=new Message(flag,"更新操作成功");
        }else{
            message=new Message(flag,"存在四种情况无法修改");
        }
//        respone.setHeader("Access-Control-Allow-Origin","*");
        return message.toJson();
    }


    /**
     * 门店经理审核房源
     * @param json{"buildingId":"","houseId": "","handleResult": ""}
     * @return
     */
    @RequestMapping(value = "/verify.do",method = RequestMethod.POST)
    @ResponseBody
    public String verify(@RequestParam(value = "json") String json,HttpServletResponse respone){
        //解析json
        JSONObject object= JSON.parseObject(json);
        int id=object.getInteger("houseId");
        int handleResult=Integer.parseInt(object.getString("handleResult"));
        String handleTime= TimeUtil.getCurrentTimeInString(TimeUtil.DATE_FORMAT_DATE);

        //调用业务
        boolean flag=houseService.verfiyHouse(id, handleTime, handleResult);

        //封装返回
        Message message;
        if(flag){
            message=new Message(true,"审核操作成功");
        }else{
            message=new Message(false,"审核操作失败");
        }
//        respone.setHeader("Access-Control-Allow-Origin","*");
        return message.toJson();
    }


    /**
     * 门店经理确定是否为精品房源
     * @param json{"houseId": "","handleResult": ""}
     * @return
     */
    @RequestMapping(value = "/recommend/verfiy.do",method = RequestMethod.POST)
    @ResponseBody
    public String recommend(@RequestParam(value = "json") String json){
        //解析json
        JSONObject object= JSON.parseObject(json);
        int id=object.getInteger("houseId");
        int handleResult=Integer.parseInt(object.getString("handleResult"));
        String handleTime= TimeUtil.getCurrentTimeInString(TimeUtil.DATE_FORMAT_DATE);

        //调用业务
        boolean flag=houseService.recommendHouse(id, handleTime, handleResult);

        //封装返回
        Message message;
        if(flag){
            message=new Message(true,"精品确认操作成功");
        }else{
            message=new Message(false,"精品确认操作失败");
        }
        return message.toJson();
    }

    /**
     * 条件查询房源
     * @param json {
    "address": {
    "province": "",
    "city": "",
    "county": "",
    "town": ""
    },
    "house": {
    "area ": "-",
    "sellType ": "",
    "isBoutique ": "",
    "isAuthentication": "",
    "type": "",
    "price": ""
    },
    "page": "",
    "size": ""
    }
     * @return
     */
    @RequestMapping(value = "/query.do",method = RequestMethod.POST)
    @ResponseBody
    public String queryHouseList(@RequestParam(value = "json") String json,
                                 HttpServletResponse respone){
        //解析json
        JSONObject object= JSON.parseObject(json);
        int size=Integer.parseInt(object.getString("size"));
        int startRow=(Integer.parseInt(object.getString("page"))-1)*size;
        Map<String,Object> addressMap=(Map<String,Object>) JSON.parse(object.getString("address"));
        Map<String,Object> houseMap=(Map<String,Object>) JSON.parse(object.getString("house"));

        //调用业务
        List<House> houseList=houseService.queryHouse(houseMap, addressMap);

        //封装
        Message message;
        if (houseList!=null&&!houseList.isEmpty()){
            int page =houseService.getPages(houseList,size);
            List<House> subHouse=houseService.subList(houseList, startRow, size);
            Map<String,Object> map=new HashMap<>();
            map.put("countPage",page);
            map.put("houseList",subHouse);
            message=new Message(true,"query success",map);
        }else{
            message=new Message(false,"query nothing");
        }
//        respone.setHeader("Access-Control-Allow-Origin","*");
        return message.toJson();
    }
}
